package likouJava;

import java.util.Arrays;

public class 冒泡排序 {
    public static void main(String[] args) {
         int[]a = {5,9,7,4,1,3,2,8};
        //int[]a = {1,2,3,4,5,6,7,8};
        bubble_v2(a);
    }

    private static void bubble_v2(int[] a) {
        int n = a.length - 1;
        while (true) {
            int last = 0; //表示最后一次交换索引的位置
            for (int i = 0; i < n; i++) {
                System.out.println("比较次数" + i);
                if (a[i] > a[i + 1]) {
                    swap(a, i, i + 1);
                    last = i;
                }
            }
            System.out.println("第轮冒泡" + Arrays.toString(a));
            n = last;
            if (n == 0){
                break;
            }
        }
    }

    private static void bubble(int[] a) {

        for (int j = 0; j < a.length - 1; j++) {
            //一轮冒泡
            boolean swapped = false;  //是否发生交换
            for (int i = 0; i < a.length - 1 - j; i++) {
                System.out.println("比较次数" + i);
                if (a[i] > a[i + 1]) {
                    swap(a, i, i + 1);
                    swapped = true;
                }
            }
            System.out.println("第" + j + "轮冒泡" +
                    Arrays.toString(a));
            if (!swapped){
                break;
            }
        }
    }

    private static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] =  temp;
    }

}
